package com.ma.api;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/products")
public class ProductController {

    private int count = 0;

    @GetMapping
    public List<Map<String, Object>> list() {
        return List.of(
                Map.of("id", 1, "name", "Laptop", "price", 9999),
                Map.of("id", 2, "name", "Mouse", "price", 199)
        );
    }

    @GetMapping("/{id}")
    public Map<String, Object> detail(@PathVariable Integer id) {
        return Map.of("id", id, "name", "Item-" + id, "price", 100 + id);
    }

    @GetMapping("/headers")
    public Map<String, String> getHeaders(@RequestHeader Map<String, String> headers) {
        return headers;
    }

    @GetMapping("/hello")
    public String hello() {
        count++;
        if (count % 2 == 0) {
            throw new RuntimeException("模拟服务异常");
        }
        return "Hello from Provider!";
    }
}

